import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { UrlService } from '../../../providers/url-service';
import { Api } from '../../../providers/api-service';
import * as $ from "jquery";
import { NewChallengePage } from '../../my-add-issuance/new-challenge/new-challenge';
import { ChallengeDetialOneFaqiPage } from '../../challenge-detial/challenge-detial-one-faqi/challenge-detial-one-faqi';
import { ChallengeDetialTwoStartPage } from '../../challenge-detial/challenge-detial-two-start/challenge-detial-two-start';
import { ChallengeDetialThreeStartPage } from '../../challenge-detial/challenge-detial-three-start/challenge-detial-three-start';
/*
 * 我的挑战
 * Ionic pages and navigation.
 */
@Component({
  selector: 'page-my-challenge',
  templateUrl: 'my-challenge.html',
})
export class MyChallengePage {
  StrDatetime = new Date();
  dateTime = Date.parse(this.StrDatetime.toString());
  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    public api: Api,
    public urlServcie: UrlService
  ) {
    this.api.writeLocalStorage("bor_tex_red2", this.doRefreshData);
    this.api.writeLocalStorage("bor_tex_red1", this.doRefreshData);
  }
  userid = this.urlServcie.getUserinfo("UserID");
  doRefreshData = {
    forbidFinite: true,
    type: true,//刷新类型
    refresher: null,
    indexPage: 1
  }
  ionViewDidLoad() {
    console.log('我的挑战 MyChallengePage');
    this.GetUserInitiateDekaronInfo(this.userid, this.doRefreshData.indexPage);
    this.GetUserJoinDekaronInfo(this.userid, this.doRefreshData.indexPage);
    this.qiehuan();
  }
  bor_tex_red = 1;
  /**
   * 发起和参与挑战切换
   */
  qiehuan(key = 1) {
    this.bor_tex_red = key;
    switch (key) {
      case 2:
        this.api.writeLocalStorage("bor_tex_red1", this.doRefreshData);
        this.doRefreshData = eval("(" + this.api.getLocalStorage("bor_tex_red2") + ")");
        break;
      case 1:
        this.api.writeLocalStorage("bor_tex_red2", this.doRefreshData);
        this.doRefreshData = eval("(" + this.api.getLocalStorage("bor_tex_red1") + ")");
        break;
      case 3:
        console.log("全部");

        break;
    }
  }

  pop_n() {
    $("#pop3").css("visibility", "visible");
    $("#pop4").css("visibility", "visible");

  }

  closePop_n() {
    $("#pop3").css("visibility", "collapse");
    $("#pop4").css("visibility", "collapse");
  }




  IsDate(item) {
    let isok = false;
    if (item.State == 10 && item.Pattern != 10 && item.joinlist.length < 2) {
      let day = true;
      let data2 = new Date();
      let starttime = new Date(item.StartDate * 1000);
      if (data2.getDate() == starttime.getDate()) {
        day = false;
      };

      if (day && starttime.getTime() > data2.getTime()) {
        isok = true;
      }
    }
    return isok;
  }


  btn1_yList = [];
  /**
   * 我的参与挑战
   */
  GetUserInitiateDekaronInfo(perId, indexpage) {
    let pageSize = 10;
    this.urlServcie.GetUserInitiateDekaronInfo(perId, indexpage, 0, pageSize)
      .then(data => {
        //console.log(data);
        if (data.Code == 0) {
          if (!data.Data.Items) {
            return false;
          }
          let list = data.Data.Items;
          if (this.doRefreshData.type) { //初始化
            this.btn1_yList = list;
          } else {
            this.btn1_yList = this.btn1_yList.concat(list);
          }
          if (list.length == pageSize) {
            this.doRefreshData.indexPage = this.doRefreshData.indexPage + 1;
          }
          if (list.length < pageSize) {
            this.doRefreshData.forbidFinite = false;
          }
        } else {

        }
      })
  }
  btn2_yList = [];
  /**
   * 我的挑战
   * @param perId 
   * @param indexpage 
   */
  GetUserJoinDekaronInfo(perId, indexpage) {
    let pageSize = 10;
    this.urlServcie.GetUserJoinDekaronInfo(perId, indexpage, 0, pageSize)
      .then(data => {
        console.log("我的参与挑战");
        console.log(data);
        if (data.Code == 0) {
          if (!data.Data.Items) {
            return false;
          }
          let list = data.Data.Items;

          if (this.doRefreshData.type) { //初始化
            this.btn2_yList = list;
          } else {
            this.btn2_yList = this.btn2_yList.concat(list);
          }

          if (list.length == pageSize) {
            this.doRefreshData.indexPage = this.doRefreshData.indexPage + 1;
          }
          if (list.length < pageSize) {
            this.doRefreshData.forbidFinite = false;
          }
        }
      })
  }


  /**
 * 下拉刷新
 * @param refresher 
 */
  doRefresh(refresher) {
    this.doRefreshData.indexPage = 1;
    this.doRefreshData.type = true;
    this.doRefreshData.forbidFinite = true;
    if (this.bor_tex_red == 1) {
      this.GetUserInitiateDekaronInfo(this.userid, 1);
    } else if (this.bor_tex_red == 2) {
      this.GetUserJoinDekaronInfo(this.userid, 1);
    }


    setTimeout(() => {
      refresher.complete();
    }, 2000);
  }
  /**
 * 上啦加载更多
 * @param infiniteScroll 
 */
  doInfinite(infiniteScroll) {

    this.doRefreshData.type = false;
    setTimeout(() => {
      if (this.bor_tex_red == 2) {
        this.GetUserInitiateDekaronInfo(this.userid, 1);
      } else if (this.bor_tex_red == 2) {
        this.GetUserJoinDekaronInfo(this.userid, 1);
      } else {

      }
      infiniteScroll.complete();
    }, 3000);
  }
  /**
   * 新建挑战
   */
  newChangell() {
    this.navCtrl.push(NewChallengePage);
  }
  openD_t_my(item) {
    let page;
    if (item.Pattern == 10) {
      page = ChallengeDetialOneFaqiPage;
    } else if (item.Pattern == 20) {
      page = ChallengeDetialTwoStartPage;
    } else if (item.Pattern == 30) {
      page = ChallengeDetialThreeStartPage;
    }
    this.navCtrl.push(page, { "params": item });
  }
}
